package cn.kgc.mybatis.batis;

import cn.kgc.mybatis.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/4/20
 * @description  实现使用持久层框架实现数据的查询
 * 每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为核心的。
 * SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。
 * 而 SqlSessionFactoryBuilder 则可以从 XML 配置文件或一个预先配置的 Configuration
 * 实例来构建出 SqlSessionFactory 实例
 */
public class TestMyBatis {

    private static SqlSessionFactory factory ;

    static {
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    }

    public static void main(String[] args) throws IOException {

        // 查询用户信息列表
        // List<User> users = selectUsers();
        // users.forEach(System.out::println);

        // User user = selectOne();
        // System.out.println("user = " + user);

        User user = new User()
                .setUsername("露西")
                .setPassword("123456")
                .setId(14);


        // Integer line = addUser(user);
        //  user  id ?   分配角色    id
        // System.out.println("user.getId() = " + user.getId());


        // Integer line = updateUser(user);

        Integer line = deleteUser(14);
        System.out.println(" update data = " + (line>0));

    }


    public static List<User> selectUsers() throws IOException {
        // 执行sql  SqlSession
        SqlSession sqlSession = factory.openSession();
        // 查询数据列表
        List<User> users = sqlSession.selectList("a.selectUsers");
        // 释放资源
        sqlSession.close();
        return  users;
    }

    // 根据用户id查询用户信息
    public static User selectOne() throws IOException {
        // 执行sql  SqlSession
        SqlSession sqlSession = factory.openSession();

        User user  = sqlSession.selectOne("a.selectUser", 1);
        sqlSession.close();
        return user;

    }

    // 根据用户id添加用户信息
    public static Integer addUser(User user) throws IOException {


        //   启动事务的自动提交
        SqlSession sqlSession = factory.openSession(true);

        int insert = sqlSession.insert("a.addUser", user);

        // 事务提交
        // sqlSession.commit();

        sqlSession.close();
        return insert;

    }


    // 根据用户id修改用户信息
    public static Integer updateUser(User user) throws IOException {


        //   启动事务的自动提交
        SqlSession sqlSession = factory.openSession(true);

        int update = sqlSession.update("a.updateUser", user);

        sqlSession.close();
        return update;

    }


    // 根据用户id修改用户信息
    public static Integer deleteUser(Integer id) throws IOException {


        //   启动事务的自动提交
        SqlSession sqlSession = factory.openSession(true);

        int delete = sqlSession.delete("a.deleteUser", id);
        sqlSession.close();
        return delete;

    }

}
